package Interfaces;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import Boundary.UpdateEventCondReact;
import Visualization.EventVis;
import calculator.auxiliary_I_Team;

public class ShowEventsAndCondReacts 
{
	JPanel container;
	ShowEventsAndCondReacts self=this;
	UpdateEventCondReact updateEventCondReact;
	JTable tab;
		
	public ShowEventsAndCondReacts(UpdateEventCondReact uECR)
	{
		updateEventCondReact = uECR;
	}
	public JPanel init()
	{
		container = new JPanel();
		container.setLayout(new BorderLayout());
		
		JLabel label = new JLabel("Show Events and Conditions-Reactions");
		label.setFont(new Font("Tahoma", Font.BOLD, 24));
		container.add(label, BorderLayout.NORTH);
		
		container.add(add_bottons(),BorderLayout.EAST);
		container.add(add_table(), BorderLayout.CENTER);
		
		return container;
	}
	
	public JPanel add_bottons()
	{
		JPanel right = new JPanel();
		right.setLayout(new GridLayout(6,0));
		
		JButton add = new JButton("Add Event");
		add.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				display(new ECR_manageElement().show(self, false, null, updateEventCondReact.availableTypes));
			}
		});
		
		JButton delete = new JButton("Delete Event");
		delete.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(tab.getSelectedRow()!=-1)
				{
					int i=0;
					while(updateEventCondReact.main.OPEN_PRJ.eventCondReact.trova_from_desc((String)tab.getValueAt(tab.getSelectedRow()-i,0))==null)
						i++;
					updateEventCondReact.deleteEvent(updateEventCondReact.main.OPEN_PRJ.eventCondReact.trova_from_desc((String)tab.getValueAt(tab.getSelectedRow()-i,0)));
				}
				else showMessage("Nothing Selected");
			}
		});
		
		JButton modify = new JButton("Manage Info");
		modify.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(tab.getSelectedRow()!=-1)
				{
					int i=0;
					while( updateEventCondReact.main.OPEN_PRJ.eventCondReact.trova_from_desc( (String)tab.getValueAt(tab.getSelectedRow()-i,0) ) == null)
						i++;
					
					display(new ECR_manageElement().show(self, true, updateEventCondReact.main.OPEN_PRJ.eventCondReact.trova_from_desc((String)tab.getValueAt(tab.getSelectedRow()-i,0)), updateEventCondReact.availableTypes));
				}
				else showMessage("Nothing Selected");
			}
		});
		
		JButton basic = new JButton("Generate Basic Event");
		basic.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				updateEventCondReact.generateBasicEV();
			}
		});
		JButton validate = new JButton("Validate");
		validate.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(tab.getSelectedRow()!=-1)
				{
					int i=0;
					while(updateEventCondReact.main.OPEN_PRJ.eventCondReact.trova_from_desc((String)tab.getValueAt(tab.getSelectedRow()-i,0))==null)
						i++; 
					updateEventCondReact.validate(updateEventCondReact.main.OPEN_PRJ.eventCondReact.trova_from_desc((String)tab.getValueAt(tab.getSelectedRow()-i,0)));
				}
				else showMessage("Nothing Selected");
			}
		});
		JButton close = new JButton("Close");
		close.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				updateEventCondReact.close();
			}
		});
		
		right.add(add);
		right.add(modify);
		right.add(delete);
		right.add(basic);
		right.add(validate);
		right.add(close);
	
		return right;	
	}
	
	@SuppressWarnings("serial")
	private JScrollPane add_table()
	{
		JPanel tabella = new JPanel();
		
		DefaultTableModel modelloRicerca = new DefaultTableModel();
		tab = new JTable(modelloRicerca){public boolean isCellEditable(int rowIndex, int vColIndex){return false;}@SuppressWarnings("unchecked")
		public Class getColumnClass(int columnIndex) {return String.class;}};
		modelloRicerca.addColumn("Event Name");
		modelloRicerca.addColumn("Condictions");
		modelloRicerca.addColumn("Reactions");		
		tab.setAutoCreateColumnsFromModel(false);								
		tab.getTableHeader().setReorderingAllowed(false);
		tab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tab.setFillsViewportHeight(true);
		int lines = 1;
			
		for (EventVis vis : updateEventCondReact.currentEVCR.getAll())
		{
			String s= "not (";
			int max =0;
			if(!vis.isCR_Empty()) max=vis.CR_size();
			for(int i = 0 ; i<max ; i++)
			{
				if(i==0)
					modelloRicerca.addRow(new Object[]{vis.getEventDescription(), vis.getConditionDescription(i), vis.getReactionDescriprion(i)});
				else
				{
					modelloRicerca.addRow(new Object[]{"", vis.getConditionDescription(i), vis.getReactionDescriprion(i)});
					if(i!=0)
						s+=")";
					s+=" or (";
				}
				if(auxiliary_I_Team.getLineCount(vis.getEventDescription(),27)>lines || auxiliary_I_Team.getLineCount(vis.getConditionDescription(i),27)>lines || auxiliary_I_Team.getLineCount(vis.getReactionDescriprion(i),27)>lines)
					lines=(int)Math.max(Math.max(auxiliary_I_Team.getLineCount(vis.getEventDescription(),27), auxiliary_I_Team.getLineCount(vis.getConditionDescription(i),27)), auxiliary_I_Team.getLineCount(vis.getReactionDescriprion(i),27));
				s+=vis.getConditionDescription(i);
				
			}
			s+=")";
			lines=(int)Math.max(auxiliary_I_Team.getLineCount(s, 27), lines);
			modelloRicerca.addRow(new Object[]{"", s, "ignored"});
		}

		tab.setRowHeight(tab.getRowHeight() * lines);
		tab.setDefaultRenderer(String.class, new MultiLineCellRenderer());
		tabella.setLayout(new BorderLayout());
		tabella.add(tab.getTableHeader(), BorderLayout.NORTH);
		tabella.add(tab, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.getViewport().add(tabella);
		
		return scrollPane;
	}
	
	public void display(JPanel daVisualizzare)
	{
		container.removeAll();
		container.add(daVisualizzare);
		container.setVisible(true);
		container.revalidate();
		
	}
	
	public JPanel showECR()
	{
		container.removeAll();
		container.add(init());
		container.setVisible(true);
		container.revalidate();
		
		return container;
	}
	
	public void showMessage(String string) 
	{
		JOptionPane.showMessageDialog(container, string, "Error", JOptionPane.WARNING_MESSAGE);
	}
}
